const request = {
    test : ()=> {
      return axios.get('/test')
    },
    //检查用户名
    checkUsername : (username) => {
      return axios.post('/cgwebservice/user/checkusername',{username})
    },
    //检查手机号码
    checkMobile : (mobile)=> {
      return axios.post('/cgwebservice/user/checkmobile',{mobile})
    },
    //发信息
    sendSms : (user) => {
      return axios.post('/cgwebservice/sms',user)
    },
    //注册
    register : (user) => {
      return axios.post('/cgwebservice/user/register',user)
    },
    //登录
    login : (user) => {
      return axios.post('/cgauthservice/auth/login',user)
    },
    // 查询品牌
    findBrand : ( categoryId ) => {
      return axios.get('/cgwebservice/brands/category/' + categoryId)
    },
    // 查询规格
    findSpec : ( categoryId ) => {
      // /cgwebservice/specifications/category/76
      return axios.get('/cgwebservice/specifications/category/' + categoryId)
    }
  }
  
  var axios = null
  export default ({ $axios, redirect }, inject) => {
  
    //添加token
    // sessionStorage获得token
    let token = sessionStorage.getItem('token')
    // 如果token存在，追加到请求头中
    if( token ) {
      $axios.setToken( token )
    }

    //处理响应异常
    $axios.onError(error => {
      // token失效，服务器响应403
      if(error.response.status === 403) {
        console.error(error.response.data)
        redirect('/login')
      }
    })

    //赋值
    axios = $axios
  
    //4) 将自定义函数交于nuxt
    // 使用方式1：在vue中，this.$request.xxx()
    // 使用方式2：在nuxt的asyncData中，content.app.$request.xxx()
    inject('request', request)
  }
  